package cc.uncarbon.module.appapi.service;


import cc.uncarbon.framework.core.exception.BusinessException;
import cc.uncarbon.module.appapi.mapper.OrdersItemMapper;
import cc.uncarbon.module.appapi.mapper.StaffMapper;

import cc.uncarbon.module.appapi.mapper.StoreMapper;
import cc.uncarbon.module.appapi.model.vo.StaffVO;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yanrumiao.entity.OrdersItemEntity;
import com.yanrumiao.entity.StaffEntity;
import com.yanrumiao.entity.StoreEntity;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.format.TextStyle;
import java.util.*;
import java.util.stream.Collectors;

import static cc.uncarbon.module.appapi.util.OrderNumberGenerator.getDateFromString;

/**
 * 技师
 */
@RequiredArgsConstructor
@Service
@Slf4j
public class StaffService {

    private final StaffMapper staffMapper;
    private final StoreMapper storeMapper;
    private final OrdersItemMapper ordersItemMapper;


    public List<StaffVO> getStaffListByStoreId(Long storeId) {

        LambdaQueryWrapper<StaffEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StaffEntity::getStoreId, storeId);
        List<StaffEntity> entityList = staffMapper.selectList(queryWrapper);
        // 如果查询结果为空，直接返回空列表
        if (entityList == null || entityList.isEmpty()) {
            return Collections.emptyList(); // 返回不可变空列表，避免不必要的对象创建
        }
        try {
            return entityList.stream().map(entity -> {
                        StaffVO staffVO = new StaffVO();
                        BeanUtil.copyProperties(entity, staffVO);
                        return staffVO;
                    })
                    .filter(Objects::nonNull)
                    .collect(Collectors.toCollection(() -> new ArrayList<>(entityList.size())));

        } catch (Exception e) {
            return Collections.emptyList(); // 发生异常时返回空列表
        }


    }

    //美容师可预约时间
    public List<Map<String, String>> getDayTime() {
        List<Map<String, String>> dates = new ArrayList<>();
        LocalDate today = LocalDate.now();
        DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("MM-dd");

        for (int i = 0; i < 7; i++) {
            LocalDate date = today.plusDays(i);
            String formattedDate = formatDateString(date, today);
            String monthDay = date.format(dateFormatter);
            String dayOfWeek = date.getDayOfWeek().getDisplayName(TextStyle.SHORT, Locale.CHINA);

            Map<String, String> dateInfo = new HashMap<>();
            dateInfo.put("date", monthDay);
            dateInfo.put("dayOfWeek", formattedDate);
            dates.add(dateInfo);
        }

        return dates;
    }

    private String formatDateString(LocalDate date, LocalDate today) {
        if (date.isEqual(today)) {
            return "今天";
        } else if (date.isEqual(today.plusDays(1))) {
            return "明天";
        } else if (date.isEqual(today.plusDays(2))) {
            return "后天";
        } else {
            return date.getDayOfWeek().getDisplayName(TextStyle.SHORT, Locale.CHINA);
        }
    }

    public List<Map<String, String>> getStaffDayTime(Long storeId, Long staffId, String date) {

        StoreEntity store = storeMapper.selectById(storeId);
        if (store == null) {
            throw new BusinessException(500, "服务异常");
        }
        List<Map<String, String>> timeSlots = new ArrayList<>();
        LocalTime startTime = LocalTime.parse(store.getOpenTime()); // 开始时间
        LocalTime endTime = LocalTime.parse(store.getCloseTime()); // 结束时间
        DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("HH:mm");
        LocalDateTime now = LocalDateTime.now();
        //格式化时间
//
        LocalDate dateTime = getDateFromString(date);

        while (!startTime.isAfter(endTime)) {
            LocalDateTime slotTime = LocalDateTime.of(dateTime, startTime);
            Map<String, String> timeInfo = new HashMap<>();

            timeInfo.put("time", startTime.format(timeFormatter));
            if (slotTime.isBefore(now)) {
                timeInfo.put("lable", "不可约");
            } else {

                OrdersItemEntity ordersItemEntity = ordersItemMapper.selectOne(new LambdaQueryWrapper<OrdersItemEntity>()
                        .eq(OrdersItemEntity::getStaffId, staffId)
                        .eq(OrdersItemEntity::getDate, dateTime)
                        .eq(OrdersItemEntity::getTime, startTime.format(timeFormatter))
                );
                if (ordersItemEntity == null) {
                    timeInfo.put("lable", "可约");
                } else {
                    timeInfo.put("lable", "不可约");
                }
            }
            timeSlots.add(timeInfo);
            startTime = startTime.plusMinutes(30);
        }
        return timeSlots;
    }


}
